home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / esc.jar / com / extensibility / util / LRUCache.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-06-30  |  1.4 KB  |  61 lines

  1. package com.extensibility.util;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Vector;
  5.  
  6. public class LRUCache {
  7.    protected static final int DEFAULT_MAX_SIZE = 32;
  8.    Vector history;
  9.    Hashtable map;
  10.    int maxSize;
  11.  
  12.    public LRUCache() {
  13.       this(32);
  14.    }
  15.  
  16.    public LRUCache(int var1) {
  17.       this.history = new Vector(var1);
  18.       this.map = new Hashtable(var1 * 2 + 1);
  19.       this.maxSize = var1;
  20.    }
  21.  
  22.    public Object get(Object var1) {
  23.       Object var2 = this.map.get(var1);
  24.       if (var1 != null) {
  25.          this.history.removeElement(var1);
  26.          this.history.addElement(var1);
  27.       }
  28.  
  29.       return var2;
  30.    }
  31.  
  32.    public Object put(Object var1, Object var2) {
  33.       Object var3 = null;
  34.       if (this.map.contains(var1)) {
  35.          var3 = this.put(var1, var2);
  36.          this.history.removeElement(var1);
  37.       } else if (this.history.size() == this.maxSize) {
  38.          this.map.remove(this.history.elementAt(0));
  39.          this.history.removeElementAt(0);
  40.       }
  41.  
  42.       this.history.addElement(var1);
  43.       this.map.put(var1, var2);
  44.       return var3;
  45.    }
  46.  
  47.    public void remove(Object var1) {
  48.       this.map.remove(var1);
  49.       this.history.removeElement(var1);
  50.    }
  51.  
  52.    public void replaceKey(Object var1, Object var2) {
  53.       Object var3 = this.get(var1);
  54.       if (var3 != null) {
  55.          this.put(var2, var3);
  56.          this.remove(var1);
  57.       }
  58.  
  59.    }
  60. }
  61.